package jhelp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Hashtable;

public class DBDataProcessor implements DataProcessor {

	@Override
	public Hashtable<String, Record> dispatchDataInHashtable(
			String dataSourcePath) {
		
		Connection con = null;
		String sourceData[] = dataSourcePath.split("#");
		String url = sourceData[0];
		String login = sourceData[1];
		String password = sourceData[2];
		Hashtable<String, Record> resultTable = new Hashtable<String, Record>();
		
		try{
			con = DriverManager.getConnection(url, login, password);
			Statement sqlStmt = con.createStatement();
			ResultSet resultSet = sqlStmt.executeQuery("SELECT * FROM dictionary");
			while(resultSet.next()){
				Record rec = new Record(resultSet.getInt(1),
										resultSet.getString(2),
										resultSet.getString(3));
				resultTable.put(rec.getTerm(), rec);
			}
			con.close();
		}
		catch (Exception e){
			e.printStackTrace();
		}
		return resultTable;
	}
}
